package com.shop.daoImpl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.shop.dao.Hibernate4BaseDao;

public class Hibernate4BaseDaoImpl<T> implements Hibernate4BaseDao<T>
{

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory = sessionFactory;
	}

	public Session getSession()
	{
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void delete(T entity)
	{

		getSession().delete(entity);

	}

	@Override
	public void delete(Class<T> clazz, Serializable id)
	{
		delete(get(clazz, id));

	}

	@Override
	@SuppressWarnings("unchecked")
	public List<T> findAll(Class<T> clazz)
	{
		return getSession().createQuery(
				"select en from " + clazz.getSimpleName() + " en").list();

	}

	@Override
	@SuppressWarnings("unchecked")
	public List<T> find(Class<T> clazz, String where)
	{
		return getSession().createQuery(
				"select en from " + clazz.getSimpleName() + " en where "
						+ where).list();
	}

	@Override
	public T get(Class<T> clazz, Serializable id)
	{
		return (T) getSession().get(clazz, id);
	}

	@Override
	public Serializable save(T entity)
	{
		return getSession().save(entity);
	}

	@Override
	public void update(T entity)
	{
		getSession().update(entity);
	}

	@Override
	public List<T> find(String hql, Object[] params)
	{
		Query query = getSession().createQuery(hql);
		setParams(query, params);
		return query.list();
	}

	private void setParams(Query query, Object[] params)
	{
		if (params != null && params.length > 0)
			for (int i = 0; i < params.length; i++)
				{
					query.setParameter(i, params[i]);
				}
	}

	@Override
	public List<T> find(String hql, List list)
	{

		return find(hql, list.toArray());
	}

	@Override
	public List<T> find(String hql, Object[] params, Integer page, Integer rows)
	{
		Query query = getSession().createQuery(hql);
		setParams(query, params);
		return query.setFirstResult((page - 1) * rows).setMaxResults(rows)
				.list();

	}

	@Override
	public List<T> find(String hql, List list, Integer page, Integer rows)
	{
		return find(hql, list.toArray(), page, rows);
	}

	@Override
	public long count(String hql)
	{
		return   (Long) getSession().createQuery(hql).uniqueResult();
	}

	@Override
	public long count(String hql, Object[] params)
	{
		Query query = getSession().createQuery(hql);
		setParams(query, params);
		return (Long) query.uniqueResult();
	}

	@Override
	public long count(String hql, List list)
	{

		return count(hql, list.toArray());
	}

	@Override
	public Integer excuteHql(String hql)
	{
		return getSession().createQuery(hql).executeUpdate();
	}

	@Override
	public Integer excuteHql(String hql, Object[] params)
	{
		Query query = getSession().createQuery(hql);
		setParams(query, params);
		return query.executeUpdate();
	}

	@Override
	public Integer excuteHql(String hql, List list)
	{
		return excuteHql(hql, list.toArray());
	}

}
